import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
X = np.array([[-1, -2], [-2, -1], [-3, -2], [1, 3], [2, 1], [3, 2]])
y = np.array([0, 0, 0, 1, 1, 1])
## 可视化构造的数据样本点
fig = plt.figure(figsize=(6,5))
plt.scatter(X[:,0],X[:,1], c=y, s=50, cmap='viridis')
plt.title('Dataset')
plt.show()
# 创建分类器
lr = LogisticRegression()
lr.fit(X,y)  # 拟合数据点

##查看其对应模型的w
print('the weight(w) of Logistic Regression:',lr.coef_)
##查看其对应模型的w0
print('the intercept(w0) of Logistic Regression:',lr.intercept_)

# 可视化决策边界
plt.figure()
plt.scatter(X[:,0],X[:,1], c=y, s=50, cmap='viridis')
plt.title('Dataset')

nx, ny = 200, 100   # 指定点个数
x_min, x_max = plt.xlim()  # x轴的边界
y_min, y_max = plt.ylim()  # y轴的边界

x_grid, y_grid = np.meshgrid(np.linspace(x_min, x_max, nx),np.linspace(y_min, y_max, ny))   # 得到所有可能坐标点组合，以linespace生成的数据点

# ravel()将n*m的数组转换为(n*m,1)，np.c_([],[])按行连接两个矩阵,
z_proba = lr.predict_proba(np.c_[x_grid.ravel(), y_grid.ravel()])   # 由此得到所有可能坐标点输入后的概率值, z_proba:(20000,2)
z_proba = z_proba[:, 1].reshape(x_grid.shape)

plt.contour(x_grid, y_grid, z_proba, 1, linewidths=2., colors='blue')  # 绘制等高线，找到所有经过逻辑回归后值相同的点，取中间的等高线

plt.show()
# 新的预测点
x_fearures_new1 = np.array([[0, -1]])
x_fearures_new2 = np.array([[1, 2]])
# 预测分类
y_label_new1_predict=lr.predict(x_fearures_new1)
y_label_new2_predict=lr.predict(x_fearures_new2)
print('The New point 1 predict class:\n',y_label_new1_predict)  # [0]
print('The New point 2 predict class:\n',y_label_new2_predict)  #
# 预测分类概率
y_label_new1_predict_proba=lr.predict_proba(x_fearures_new1)
y_label_new2_predict_proba=lr.predict_proba(x_fearures_new2)
print('The New point 1 predict Probability of each class:\n',y_label_new1_predict_proba)
print('The New point 2 predict Probability of each class:\n',y_label_new2_predict_proba)
from sklearn.datasets import load_iris
import pandas as pd

iris = load_iris() #得到数据特征，包含data,target,feature_names, target_names,DESCR等信息
df_X = pd.DataFrame(data=iris.data, columns=iris.feature_names)
y = iris.target
df_X.describe()
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
X_train,X_test,y_train,y_test = train_test_split(df_X,y, test_size=0.2, random_state=20200816)
lr = LogisticRegression()
lr.fit(X_train,y_train)
print('the weight(w) of Logistic Regression:\n',lr.coef_)
print('the intercept(w0) of Logistic Regression:\n',lr.intercept_)
from sklearn import metrics
y_train_pred=lr.predict(X_train)
y_test_pred=lr.predict(X_test)
print('The accuracy on train set is:',metrics.accuracy_score(y_train,y_train_pred))
print('The accuracy on test set is:',metrics.accuracy_score(y_test,y_test_pred))
confusion_matrix_result=metrics.confusion_matrix(y_test,y_test_pred)
print('The confusion matrix result:\n',confusion_matrix_result)
plt.figure(figsize=(8,6))
sns.heatmap(confusion_matrix_result,annot=True,cmap='Blues')
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.show()